package ctx

import (
	"go-init/dao"
	"go-init/lib/log"
	"strconv"
	"strings"

	"github.com/gin-contrib/sessions"
)

// QueryInt parse query string to int
func (c *Context) QueryInt(key string) int {
	qs := c.Query(key)
	qi, err := strconv.Atoi(qs)
	if err != nil {
		return 0
	}
	return qi
}

// GetAdminId according session get uid
func (c *Context) QuerySet(query *dao.Qs, querykey, key string) {
	value := c.Query(key)
	if value != "" {
		query.Add(querykey, value)
	}
}

// GetAdminId according session get uid
func (c *Context) QuerySetArray(query *dao.Qs, s []string) {
	for _, v := range s {
		c.QuerySet(query, "a."+v+"=?", v)
	}
}

// GetAdminId according session get uid
func (c *Context) QuerySetMap(query *dao.Qs, m map[string]string) {
	for k, v := range m {
		c.QuerySet(query, k, v)
	}
}

// GetAdminId according session get uid
func (c *Context) QuerySetLike(query *dao.Qs, querykey, key string) {
	value := c.Query(key)
	if value != "" {
		query.Add(querykey+" like ? ", "%"+value+"%")
	}
}

// GetAdminId according session get uid
func (c *Context) QuerySetLikeArray(query *dao.Qs, s []string) {
	for _, v := range s {
		c.QuerySetLike(query, "a."+v, v)
	}
}

// GetAdminId according session get uid
func (c *Context) QuerySetLikeL(query *dao.Qs, querykey, key string) {
	value := c.Query(key)
	if value != "" {
		query.Add(querykey+" like ? ", value+"%")
	}
}

// GetAdminId according session get uid
func (c *Context) QuerySetLikeLArray(query *dao.Qs, s []string) {
	for _, v := range s {
		c.QuerySetLikeL(query, v, v)
	}
}

// GetAdminId according session get uid
func (c *Context) GetAdminId() int {
	session := sessions.Default(c.Copy())
	user := session.Get("admin")
	uid, ok := user.(int)
	if !ok {
		return 0
	}
	return uid
}

// GetAdminId according session get uid
func (c *Context) GetUID() int {
	session := sessions.Default(c.Copy())
	user := session.Get("user")
	uid, ok := user.(int)
	if !ok {
		return 0
	}
	return uid
}

// PageHandle handlepage
func (c *Context) PageHandle() (o dao.Other) {
	page := c.Query("pageNo")
	pageSize := c.Query("pageSize")
	pageno, _ := strconv.Atoi(page)
	limit, _ := strconv.Atoi(pageSize)
	if limit < 1 {
		limit = 10
	}
	o.Offset = pagechange(pageno, limit)
	o.Limit = limit
	return
}

func (c *Context) IsWechat() bool {
	return strings.Split(c.Context.Request.RequestURI, "/")[2] == "wechat"
}

// pagechange 分页
func pagechange(now int, num int) (page int) {
	if now == 0 {
		now = 1
	}
	if num == 0 {
		num = dao.DefaultPageSize
	}
	page = (now - 1) * num
	return
}

// HTTPError example
type HTTPError struct {
	Code    int    `json:"code" example:"400"`
	Status  int    `json:"status" example:"1"`
	Message string `json:"message" example:"status bad request"`
}

// NewError example
func NewError(c *Context, status int, err error) {
	er := HTTPError{
		Code:    status,
		Status:  0,
		Message: err.Error(),
	}
	c.JSON(status, er)
}

type R struct {
	Status int         `json:"status"`
	Msg    string      `json:"msg"`
	ErrMsg error       `json:"errmsg"`
	Data   interface{} `json:"data"`
}

func (c *Context) HandlerError(err error) bool {
	if err != nil {
		log.Error2(err)
		r := R{Msg: err.Error(), Status: 0}
		c.AbortWithStatusJSON(400, r)
		return true
	}
	return false
}

type ListData struct {
	Data       interface{} `json:"data"`
	TotalCount int         `json:"totalCount"`
}
